<?php
/*\
 *	oh.other.include.php
 *	--------------------
 *
 *
 *
\*/

function oh_key_of($array,$value) {
	foreach($array as $k=>$v) {
		if ($value == $v) return $k;
	}
	return null;
}

function oh_any($array,$selector="") {
	$keys = array_keys($array);
	return in_array($selector,$array) ? $selector : $array[$keys[0]];
}

function oh_get($index,$array) {
	return isset($_GET[$index])&&in_array($_GET[$index],$array) ? $_GET[$index] : $array[0];
}

function oh_is_valid_function($s) {
	if (preg_match("/^[a-z_][a-z0-9_]*$/i",$s)) {
		if (function_exists($s)) {
			return true;
		}
	} else {
		return false;
	}
	return false;
}

function oh_methods($module = "") {
	if ($module == "") {
		$f = get_defined_functions();
		$f = $f["user"];
		$o = array();
		foreach($f as $i) {
			if (substr($i,0,3) == "oh_") {
				$o[] = $i;
			}
		}
		asort($o);
		return $o;
	} else {
		oh($module);
		$f = get_class_methods("oh_class_".$module);
		asort($f);
		return $f;
	}
}	

function oh_string_to_array($s) {
	$s = explode("," , $s);
	$o = array();
	foreach($s as $i) {
		$i = explode("=>",$i);
		$o[trim($i[0])] = trim($i[1]);
	}
	return $o;
}

function oh_module_header($config="") {
	
}

function oh_get_file_permission($file) {
	return substr(sprintf('%o', fileperms($file)), -4);				
}		

function oh_wellcome() {
	return '
		<style type="text/css">
			body {margin:0;padding:0;}
			#h1	 {padding:10px;background:#507AAA;color:#fff;font-family:Arial,Helvetica,sans-serif;}
		</style>
		<div id="h1"><small>Wellcome to </small>oh.framework</div>
	';
}

function oh_datasource_find($modules) {
	foreach($modules as $module) {
		//if (oh("debug.modules.model"))
		//	oh_message("Test module '{$module}' to find 'model datasource'");
		
		if (oh_module_exists($module)) {
			
			if (oh($module)->is_ready()) {
				return $module;
			}
		}
	}
	//oh_message(OH_EM002.implode(", ",$modules));
	return null;
}

function oh_implode_key($s,$array,$key) {
	$o = array();		
	foreach($array as $item) {
		if (isset($item->$key)) {
			$o[] = $item->$key;
		} else if (isset($item[$key])) {
			$o[] = $item[$key];
		}
	}
	return implode($s,$o);
}	

function oh_stop() {
	die();
}

function oh_menu_item($k,$v) {
	$item = $k.' '.$v;
	preg_match("/([a-z]+)/",$item,$key);			
	$key=isset($key[0])?$key[0]:"";
	preg_match("/\[([a-zA-Z]*)\]/",$item,$label);	$label=isset($label[1])?$label[1]:"";	
	$url = $item;	
	$url = preg_replace("/$key/"," ",$url,1);
	$url = str_replace("[$label]"," ",$url);
	return array(
		"url" 	=> trim($url),
		"key" 	=> $key,
		"label" => $label,
	);
}


